var baseURL = ctx + "/activiti/model";
$(function() {
	load();
});

function load() {
	$("#jqGrid").jqGrid({
		url: baseURL + '/list',
		datatype: "json",
		colModel: [
			{ label: '模型id', name: 'id', index: "id", width: 45, key: true,search: false },
			{ label: '模型标识', name: 'key', width: 75,search: false },
			{ label: '模型名称', name: 'name', sortable: false, width: 75 },
			{ label: '版本号', name: 'version', width: 90,search: false },
			{ label: '创建时间', name: 'createTime', width: 100,search: false },
			{ label: '最后更新时间', name: 'lastUpdateTime', width: 100,search: false },
			{ label: '导出xml', name: 'rowId', width: 100, search: false,formatter:function(value, row, index){
				return '<a href="'+baseURL+'/export/'+row.rowId+'" target="_blank">xml</a>';
			}}
		],
		viewrecords: true,
		height: 'auto',
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
		}
	});
	$("#jqGrid").filterToolbar({}).jqGrid('navGrid',"#jqGridPager",{//刷新按钮
		refresh: true,
		edit:false,del: false,search:false,add:false
	});
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
		},
		showList: true,
		title:null,
		task:{
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function (){
			window.open(baseURL + '/add');
		},
		update: function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			window.open(baseURL + '/edit/' + id);
		},
		deploy: function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			confirm('确定要部署选中的模型吗？',function() {
				$.ajax({
					url : baseURL+"/deploy/"+id,
					type : "post",
					data : {
						'id' : id
					},
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功', function(){
								vm.reload();
							});
						} else {
							alert(r.message);
						}
					}
				});
			})
		},
		del: function () {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type : 'POST',
					data : {
						"ids" : userIds
					},
					url : baseURL + '/batchRemove',
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功', function(){
								vm.reload();
							});
						} else {
							alert(r.message);
						}
					}
				});
			});
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{},
				page:page
			}).trigger("reloadGrid");
		}
	}
});